home *** CD-ROM | disk | FTP | other *** search
- /* readdir routine */
-
- /* under MiNT (v0.9 or better) these use the appropriate system calls.
- * under TOS or older versions of MiNT, they use Fsfirst/Fsnext
- *
- * Written by Eric R. Smith and placed in the public domain
- */
-
- #include <stdlib.h>
- #include <string.h>
- #include <types.h>
- #include <limits.h>
- #include <dirent.h>
- #include <errno.h>
- #include <osbind.h>
- #include <mintbind.h>
- #include "lib.h"
-
- extern int __mint;
- extern ino_t __inode; /* in stat.c */
-
- struct dirent *
- readdir(d)
- DIR *d;
- {
- struct dbuf {
- long ino;
- char name[NAME_MAX + 1];
- } dbuf;
- long r;
- _DTA *olddta;
- struct dirent *dd = &d->buf;
-
- if (__mint > 8) {
- r = (int)Dreaddir((int)(NAME_MAX+1+sizeof(long)), d->handle, (char *) &dbuf);
- if (r == -ENMFIL)
- return 0;
- else if (r) {
- errno = (int) -r;
- return 0;
- }
- dd->d_ino = dbuf.ino;
- dd->d_off++;
- dd->d_reclen = (short)strlen(dbuf.name);
- strcpy(dd->d_name, dbuf.name);
- return dd;
- }
- /* ordinary TOS search, using Fsnext. Note that the first time through,
- * Fsfirst has already provided valid data for us; for subsequent
- * searches, we need Fsnext.
- */
- if (d->status == _NMFILE)
- return 0;
- if (d->status == _STARTSEARCH) {
- d->status = _INSEARCH;
- } else {
- olddta = Fgetdta();
- Fsetdta(&(d->dta));
- r = Fsnext();
- Fsetdta(olddta);
- if (r == -ENMFIL) {
- d->status = _NMFILE;
- return 0;
- } else if (r) {
- errno = (int)-r;
- return 0;
- }
- }
- dd->d_ino = __inode++;
- dd->d_off++;
- _dos2unx(d->dta.dta_name, dd->d_name);
- dd->d_reclen = (short)strlen(dd->d_name);
- return dd;
- }
-